/**
 * 
 */
package cn.xiaolang.function.json.jackson;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

/**
 * TODO..
 * 
 * @version 1.5
 * @author fuhw
 * @date 2017年9月21日
 */
public class DemoApp {

	public static void main(String[] args) throws Exception {

		ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
		AccessToken token = new AccessToken("Sfsdfklslfs", "xiaolang");
		System.err.println("toString - >" + token.toString());
		System.err.println("toJson - >\n" + objectMapper.writeValueAsString(token));
		System.err.println("toObject-byjson - >"
				+ objectMapper.readValue(objectMapper.writeValueAsString(token), AccessToken.class));

		ObjectMapper xmlMapper = new XmlMapper().enable(SerializationFeature.INDENT_OUTPUT);
		System.err.println("toXml - >\n" + xmlMapper.writeValueAsString(token));
		System.err.println(
				"toObject-byxml - >" + xmlMapper.readValue(xmlMapper.writeValueAsString(token), AccessToken.class));

		AccessTemp accessTemp = new AccessTemp("xiaolang");
		System.err.println(accessTemp.toString());
		System.err.println(objectMapper.writeValueAsString(accessTemp));
		System.err.println(objectMapper.readValue(objectMapper.writeValueAsString(accessTemp), AccessTemp.class).getCreateTime());
	}

}

class AccessTemp {
	private String username;
	
	@JsonDeserialize(using = DateDeserializer.class)
	@JsonSerialize(using = DateSerializer.class)
//	@JsonFormat(pattern="yyyy-MM-dd")//测试有问题
	private Date createTime;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public AccessTemp(String username) {
		super();
		this.username = username;
		this.createTime = new Date();
	}

	public AccessTemp() {
		super();
	}

	@Override
	public String toString() {
		return "AccessTemp [username=" + username + ", createTime=" + createTime + "]";
	}
	
	
	
}


@JacksonXmlRootElement(localName = "xml")
class AccessToken {

	@JsonProperty("access_token")
	@JacksonXmlProperty(localName = "AccessToken")
	@JacksonXmlCData
	private String token;

	@JsonProperty("refresh_token")
	private String name;

	@JacksonXmlProperty(localName = "CreateTime")
	@JsonDeserialize(using = DateDeserializer.class)
	@JsonSerialize(using = DateSerializer.class)
	private Date createTime;

	public AccessToken(String token, String name) {
		super();
		this.token = token;
		this.name = name;
		this.createTime = new Date();
	}

	public AccessToken() {
		super();
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

}